#include "qmylineedit.h"

#include "../../include/abstractproperty.h"
#include "../../lib/shared/qdatamanager.h"
#include "../../include/qcommonstruct.h"

#include <QVariant>
#include <QMouseEvent>

QLineEditHost::QLineEditHost(QWidget *wid):
    QObjectHost(wid)
{

}

QString QLineEditHost::text()
{
    return ((QLineEdit*)m_widget)->text();
}

QMyLineEdit::QMyLineEdit(QWidget *parent):
    QLineEdit(parent),
    m_host(new QLineEditHost(this))
{
    setProperty("className","QLineEdit");
    setProperty("typeName","Line Edit");
    setProperty("typeIcon",":/images/widgets/lineedit.png");

    m_host->addProperty("Attribute",QStringList()<<"font"<<"focusPolicy"<<"text"<<"readOnly"<<"frame"<<"maxLength");
    m_host->addProperty("StyleSheet",QStringList()<<"background-color"<<"background-image");
    m_host->addProperty("Signals",QStringList()<<"editingFinished");
    m_host->addUserProperty(QStringList()<<"text");

    connect(this,SIGNAL(editingFinished()),this,SLOT(finishEditing()));
    connect(this,SIGNAL(textChanged(QString)),this,SLOT(textChanged_signal()));
}

void QMyLineEdit::finishEditing()
{
    QString code=property("editingFinished").toString();
    if(code!="")
    {
        QString para=QString("type=editingFinished,num=%1,_text=%2").arg(1).arg(this->text());
        m_host->exec(code,para);
    }

}

void QMyLineEdit::textChanged_signal()
{
    if(property("running").toBool())
    {
        QDataManager *manager=QDataManager::manager();
        MemeryValueItems items=property("memeryValue").value<MemeryValueItems>();
        foreach(tagMemeryValueInfo info,items)
        {
            if(info.m_propertyName=="text")
            {
                manager->setUserData(info.m_valueName,text());

            }
        }
    }
}

void QMyLineEdit::updateProperty(const QString & name)
{
    if(name=="Z-Value")
    {
        emit zValueChanged();
    }
}

void QMyLineEdit::updatePropertyAttribute()
{
    m_host->updatePropertyAttribute();
    PropertyList list=property("property_list").value<PropertyList>();

    QString name;

    foreach(QAbstractProperty *p,list)
    {
        name=p->name();

        if(name=="editingFinished")
        {
            p->setAttribute("param","editingFinished");
        }
    }
}

void QMyLineEdit::initProperty()
{
    if(property("running").toBool())
    {
        m_host->setGlobalObject(sender());
        m_host->initRunningProperty();
    }
}
